//
// Created by 吴喆 on 2020/3/16.
//

#include "DirectionStateMachine.h"

DirectionStateMachine::DirectionStateMachine() {
    currentState = new RightState();
}

/*NOLINT*/

PlayableState *RightState::handleInput(Playable &playable, KeyEvent *event) {
    if (event->key == GLFW_KEY_LEFT && event->down) {
        return new LeftState();
    }
    return nullptr;
}

PlayableState *RightState::update(Playable &playable) {
    if (Input::getInstance()->keyMap[GLFW_KEY_LEFT] && Input::getInstance()->keyMap[GLFW_KEY_LEFT]->held && !Input::getInstance()->keyMap[GLFW_KEY_RIGHT]->held) {
        return new LeftState();
    }
    return nullptr;
}

void RightState::enter(Playable &playable) {
    playable.direction = DIRECTION::RIGHT;
}

PlayableState *LeftState::handleInput(Playable &playable, KeyEvent *event) {
    if (event->key == GLFW_KEY_RIGHT) {
        return new RightState();
    }
    return nullptr;
}

PlayableState *LeftState::update(Playable &playable) {
    if (Input::getInstance()->keyMap[GLFW_KEY_RIGHT] && Input::getInstance()->keyMap[GLFW_KEY_RIGHT]->held &&!Input::getInstance()->keyMap[GLFW_KEY_LEFT]->held) {
        return new RightState();
    }
    return nullptr;
}

void LeftState::enter(Playable &playable) {
    playable.direction = DIRECTION::LEFT;
}
